<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/9/18 10:26
 */

namespace App\Controller\Amount;


use App\Controller\ApiController;
use App\Request\Amount\AmountLogEditRequest;
use App\Request\Amount\AmountLogInfoRequest;
use App\Request\Amount\AmountLogRemoveRequest;
use App\Request\Amount\AmountLogSaveRequest;
use App\Request\Amount\AmountLogsRequest;
use App\Request\Amount\MonthTotalRequest;
use App\Service\Amount\AmountLogService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

class AmountLogController extends ApiController
{

    #[Inject]
    protected AmountLogService $amountLogService;

    /**
     * 保存动账记录
     * @param AmountLogSaveRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function saveAmountLog(AmountLogSaveRequest $request)
    {
        $data = $request->validated();

        $amountLogs = $this->amountLogService->amountLogSave($data);

        return $this->responseOk($amountLogs);
    }

    public function editAmountLog(AmountLogEditRequest $request)
    {
        $data = $request->validated();

        $amountLogs = $this->amountLogService->editAmountLog($data);

        return $this->responseOk($amountLogs);
    }

    public function getMonthTotal(MonthTotalRequest $request)
    {
        $data = $request->validated();

        $month = empty($data['date']) ? date("Y-m") : $data['date'];

        $accountId = $data['account_id'] ?? null;
        list($inAmount,$outAmount,$nowAmount) = $this->amountLogService->monthAmountTotal($month,$accountId);

        return $this->responseOk([
            'in' => $inAmount,
            'out' => $outAmount,
            'now' => $nowAmount,
        ]);
    }


    /**
     * 动账记录详细
     * @param AmountLogInfoRequest $request
     * @return ResponseInterface
     */
    public function logInfo(AmountLogInfoRequest $request)
    {
        $data = $request->validated();

        $amountLog = $this->amountLogService->amountLogInfo($data['log_id']);

        return $this->responseOk($amountLog);
    }


    /**
     * 移除日志记录
     * @param AmountLogRemoveRequest $request
     * @return ResponseInterface
     */
    public function removeLogs(AmountLogRemoveRequest $request)
    {
        $data = $request->validated();

        $amountLogs = $this->amountLogService->removeLog($data['id']);

        return $this->responseOk($amountLogs);
    }

    /**
     * 获取动账记录
     * @param AmountLogsRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function amountLogs(AmountLogsRequest $request)
    {
        $data = $request->validated();

        $data['limit'] = empty($data['limit']) ? 20 : $data['limit'];
        $amountLogs = $this->amountLogService->amountLogsList($data);
        return $this->responseOk($amountLogs);

    }
}